package com.tom.controller;

import com.tom.pojo.Address;
import com.tom.pojo.User;
import com.tom.service.IMainService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/main")
public class MainController {
    @Resource
    private IMainService mainService;

    /**
     * 登录
     * @param username  用户名
     * @param password  密码
     * @param usertype  用户类型
     * @return  登录结果,用户对象
     */
    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(
            @RequestParam String username,
            @RequestParam String password,
            @RequestParam String type
            ) {

        Byte typeb = Byte.valueOf(type);

        User userp = new User();
        userp.setUsername(username);
        userp.setPassword(password);
        userp.setType(typeb);

//        System.out.println(userp.getUsername()+userp.getPassword()+userp.getId());
        User user = mainService.login(userp);
//        System.out.println(user);
        Map<String, Object> loginMsg = new HashMap<>();

        if (user==null){
            // 登录失败
            loginMsg.put("success", false);
//            System.out.println("false");
        } else {
            // 登录成功
            loginMsg.put("success", true);
            loginMsg.put("user", user);
//            System.out.println("true");
        }
        return loginMsg;

    }

    /**
     * 注册
     * @param user  用户参数
     * @return  注册结果
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> register(@RequestBody User user) {
        int result = mainService.register(user);
//        System.out.println("result: " + result);

        Map<String, Object> registerMsg = new HashMap<>();
        if (result>0){
            // 注册成功
            registerMsg.put("success", true);
        } else {
            // 注册失败
            registerMsg.put("success", false);
        }
        return registerMsg;
    }



}
